package com.briup.bookstore.web.controller;

import com.briup.bookstore.common.response.Result;
import com.briup.bookstore.domain.dto.OrderCreateDTO;
import com.briup.bookstore.domain.vo.OrderDetailVO;
import com.briup.bookstore.domain.vo.OrderVO;
import com.briup.bookstore.service.OrderService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;

    @GetMapping("/")
    public Result<List<OrderVO>> list(String id, Integer status) {
        List<OrderVO> orderVOS = orderService.queryByConditional(id, status);
        return Result.success(orderVOS)
                .apply("查询订单成功");
    }

    @GetMapping("/{orderId}")
    public Result<OrderDetailVO> getOrderDetail(@PathVariable String orderId){
        OrderDetailVO orderVO = orderService.queryById(orderId);
        return Result.success(orderVO)
                .apply("查询订单成功");
    }

    @PostMapping
    public Result<?> addOrder(@RequestBody @Valid OrderCreateDTO orderCreateDTO){
        orderService.addOrder(orderCreateDTO);
        return Result.success();
    }

    @PutMapping("/close/{orderId}")
    public Result<?> closeOrder(@PathVariable String orderId){
        orderService.closeOrder(orderId);
        return Result.success();
    }

    @PutMapping("/complete/{orderId}")
    public Result<?> completeOrder(@PathVariable String orderId){
        orderService.completeOrder(orderId);
        return Result.success();
    }
}
